#include "stdafx.h"
#include "ModelNode.h"

#include "MeshInstance.h"
#include "Mesh.h"
#include "RigidBody.h"

#include "Scene.h"
#include "RenderingSystem.h"

#include "Ray.h"

using namespace mscene;


ModelNode::ModelNode(Scene & scene, PrimitiveType type, const char * name)
	: Node(scene, name), PrimType(type), mModel(NULL)
{
	Type = MODEL;
}

ModelNode::ModelNode( const ModelNode & src )
	: Node(src), PrimType(src.PrimType)
{
	setModel(*src.mModel->copy());
}

ModelNode::~ModelNode()
{
	mScene.RenderSys.removeGfxObject(*mModel);
}

ModelNode* ModelNode::copy()
{
	return new ModelNode(*this);
}

void ModelNode::update( float dt )
{
	Node::update(dt);
	if(mModel != NULL)
	{
		mModel = (mgfx::MeshInstance*)&mScene.RenderSys.findGfxObject(mModelId);
		mModel->updateDrawData(Position, Orientation, Scale);
	}
}

float ModelNode::rayIntersection( const mmath::Ray & ray )
{
	return mRigidBody->checkRayIntersection(ray);
}

void ModelNode::setModel(mgfx::MeshInstance & model)
{
	mModel = &model;
	mModelId = model.Id;
}

const mgfx::MeshInstance & ModelNode::getModel() const
{
	return *mModel;
}

mgfx::MeshInstance & ModelNode::getModelEditable()
{
	return *mModel;
}
